package com.zhk.ScriptManagement.controller;

import com.zhk.ScriptManagement.entity.Order;
import com.zhk.ScriptManagement.service.OrderService;
import com.zhk.ScriptManagement.util.CodeMsg;
import com.zhk.ScriptManagement.util.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Optional;

/**
 * @Author: zhouhongkun
 * @Description: 订单controller
 * @Date: 2021/4/5 21:02
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     * @param order
     * @return
     */
    @PostMapping("addOrder")
    public Result addOrder(@RequestBody Order order){
        if(!Optional.ofNullable(order).isPresent()){
            return Result.error(CodeMsg.PARAMETER_ISNULL,"参数不能为空");
        }
        return orderService.addOrder(order);
    }

    /**
     * 修改订单
     * @param order
     * @return
     */
    @PostMapping("updateOrder")
    public Result updateOrder(@RequestBody Order order){
        if(!Optional.ofNullable(order).isPresent()){
            return Result.error(CodeMsg.PARAMETER_ISNULL,"参数不能为空");
        }
        return orderService.updateOrder(order);
    }

    /**
     * 删除订单
     * @param order
     * @return
     */
    @PostMapping("deleteOrder")
    public Result deleteOrder(@RequestBody Order order){
        if(!Optional.ofNullable(order).isPresent()){
            return Result.error(CodeMsg.PARAMETER_ISNULL,"参数不能为空");
        }
        return orderService.deleteOrder(order);
    }

    /**
     * 获取订单列表
     * @return
     */
    @GetMapping("getOrder")
    public Result getOrder(@RequestParam(value = "size",defaultValue = "1",required = false) String size,
                           @RequestParam(value = "num",defaultValue = "10",required = false) String num){
        return orderService.getOrder(size,num);
    }

    /**
     * 获取订单
     * @param orderId
     * @return
     */
    @GetMapping("getOrderByOrderId")
    public Result getOrderByOrderId(@RequestParam("orderId") Integer orderId){
        if(!Optional.ofNullable(orderId).isPresent()){
            return Result.error(CodeMsg.PARAMETER_ISNULL,"参数不能为空");
        }
        return orderService.getOrderByOrderId(orderId);
    }

}
